package csc.fresher.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.codec.Base64;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import csc.fresher.bankservice.BankService;
import csc.fresher.domain.Role;
import csc.fresher.domain.Status;
import csc.fresher.domain.UserInfo;

/**
 * Create new User for User Admin and User support
 * 
 *
 */
@Controller
@RequestMapping(value = "/create")
public class CreateController {

	@Autowired
	private BankService bankService;

	@ModelAttribute("user")
	private UserInfo contructUserInfo() {
		return new UserInfo();
	}

	@RequestMapping
	public String goCreate() {
		HomeController.page = "create";
		return "create";
	}

	// order: model => valid => binding => @Request
	@RequestMapping(value = "/newuser", method = RequestMethod.POST)
	public String createNewUser(
			Model model,
			@Valid @ModelAttribute("user") UserInfo user,
			BindingResult rs,
			@RequestParam(value = "fileUpload", required = false) MultipartFile file,
			@RequestParam(value = "password_again", required = false) String passwordAgain) {
		if (rs.hasErrors()) {
			return "create";
		}
		
		if(!user.getPassword().equals(passwordAgain)){
			model.addAttribute("message", "Password and repeat password must the same");
			return "create";
		}
		
		Role role = bankService.getRole(user.getRole().getRoleid());
		Status status = bankService
				.getStatus(user.getStatus().getStatusid());

		if (!file.isEmpty()) {
			byte[] image = null;
			try {
				image = file.getBytes();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			user.setImage(image);
		}
		user.setRole(role);
		user.setStatus(status);
		
		user.setPassword(bankService.hashPasswordSHA(user.getPassword()));
				
		bankService.save(user);
		model.addAttribute("message", "Create successfull");
		return "create";
	}

	@RequestMapping(value = "/checkLoginId", method = RequestMethod.POST)
	@ResponseBody
	public boolean checkLoginId(@RequestParam String loginId) {
		UserInfo user = bankService.getUserInfo(loginId);
		if (user == null) {
			return true;
		} else {
			return false;
		}
	}
	
	@RequestMapping(value="/checkcardnumber", method = RequestMethod.POST)
	@ResponseBody
	public boolean checkCardNumberId(@RequestParam String cardnumberid){
		if(cardnumberid.matches("\\d{9}")){
			return true;
		} else {
			return false;
		}
	}
	
	@RequestMapping(value = "/checkfirstname", method=RequestMethod.POST)
	@ResponseBody
	public boolean checkFistName(@RequestParam String firstname){
		if(firstname.matches("\\D+")){
			return true;
		} else {
			return false;
		}
	}
	
	@RequestMapping(value = "/checklastname", method=RequestMethod.POST)
	@ResponseBody
	public boolean checkLastName(@RequestParam String lastname){
		if(lastname.matches("\\D+")){
			return true;
		} else {
			return false;
		}
	}
	
	@RequestMapping(value = "/checkmidname", method=RequestMethod.POST)
	@ResponseBody
	public boolean checkMidName(@RequestParam String midname){
		if(midname.matches("\\D+")){
			return true;
		} else {
			return false;
		}
	}
	
	@RequestMapping(value="/checkphonefirst", method=RequestMethod.POST)
	@ResponseBody
	public boolean checkPhoneFirst(@RequestParam String phone1){
		if(phone1.matches("\\d{10}")){
			return true;
		} else if(phone1.matches("\\d{11}")){
			return true;
		} else {
			return false;
		}
	}
	
	@RequestMapping(value="/checkphonesecond", method=RequestMethod.POST)
	@ResponseBody
	public boolean checkPhoneSecond(@RequestParam String phone2){
		if(phone2.matches("\\d{10}")){
			return true;
		} else if(phone2.matches("\\d{11}")){
			return true;
		} else {
			return false;
		}
	}

}
